#include <QValidator>
#include "PatientInfoInput.h"
#include "ui_PatientInfoInput.h"
#include "DateValidator.h"

const QString strReg("(\\d{6})(-\\d{7})?");

PatientInfoInput::PatientInfoInput(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::PatientInfoInput)
{
	ui->setupUi(this);

	ui->gridLayout->setAlignment(ui->btnSearch, Qt::AlignCenter);
	ui->gridLayout->setAlignment(ui->btnRegister, Qt::AlignCenter);
	ui->txtBirth->setValidator(new DateValidator(this));
	ui->txtCaptureDate->setValidator(new DateValidator(this));
	ui->txtIDNumber->setValidator(new QRegExpValidator(QRegExp(strReg), this));

	connect(ui->btnSearch, SIGNAL(clicked()), SLOT(SlotSearch()));
	connect(ui->btnRegister, SIGNAL(clicked()), SLOT(SlotRegister()));
	connect(ui->txtIDNumber, SIGNAL(textChanged(QString)), SLOT(SlotUpcateBirth()));
	connect(ui->txtName, SIGNAL(textChanged(QString)), SLOT(SlotCheckValid()));
	connect(ui->txtIDNumber, SIGNAL(textChanged(QString)), SLOT(SlotCheckValid()));
	connect(ui->txtBirth, SIGNAL(textChanged(QString)), SLOT(SlotCheckValid()));

	this->SlotCheckValid();
}

PatientInfoInput::~PatientInfoInput()
{
	delete ui;
}

void PatientInfoInput::initField()
{
	qDebug("[init field]");
	ui->txtAge->clear();
	ui->txtBirth->clear();
	ui->txtChart->clear();
	ui->txtIDNumber->clear();
	ui->txtName->clear();
	ui->txtPhone->clear();
	ui->txtName->setFocus();
}

void PatientInfoInput::SlotSearch()
{
	SPatient patient;

	patient.m_strChart = ui->txtChart->text().trimmed();
	patient.m_strName = ui->txtName->text().trimmed();
	patient.m_strIDNumber = ui->txtIDNumber->text().trimmed();
	patient.m_dateLastCapture = QDateTime::fromString(ui->txtCaptureDate->text(), strDateFormat);

	emit SignalSearch(patient);
}

void PatientInfoInput::SlotRegister()
{
	SPatient patient;

	patient.m_strName = ui->txtName->text().trimmed();
	patient.m_iSex = ui->rdoM->isChecked() ? eMale : ui->rdoF->isChecked() ? eFemale : eUnknown;
	patient.m_dateBirth = QDate::fromString(ui->txtBirth->text().trimmed(), strDateFormat);
	patient.m_strIDNumber = ui->txtIDNumber->text().trimmed();
	patient.m_strPhone = ui->txtPhone->text().trimmed();

	emit SignalRegister(patient);
}

void PatientInfoInput::SlotUpcateBirth()
{
	QRegExp reg(strReg);

	if(reg.exactMatch(ui->txtIDNumber->text()))
	{
		qDebug() << reg.cap(1) << QDate::fromString(reg.cap(1), "yyMMdd") << QDate::fromString(reg.cap(1), "yyMMdd").toString("yyyy-MM-dd");
		ui->txtBirth->setText(QDate::fromString(reg.cap(1), "yyMMdd").toString("yyyy-MM-dd"));
	}
}

void PatientInfoInput::SlotCheckValid()
{
	qDebug() << "[check valid]";
	QString strID(ui->txtIDNumber->text());
	QString strBirth(ui->txtBirth->text());
	int iIndex(0);

	qDebug() << "[register valid check]" <<
				!ui->txtName->text().trimmed().isEmpty() <<
				(ui->txtIDNumber->validator()->validate(strID, iIndex) == QValidator::Acceptable) <<
				(ui->txtBirth->validator()->validate(strBirth, iIndex) == QValidator::Acceptable);
	ui->btnRegister->setEnabled(
				!ui->txtName->text().trimmed().isEmpty() &&
				ui->txtIDNumber->validator()->validate(strID, iIndex) == QValidator::Acceptable &&
				ui->txtBirth->validator()->validate(strBirth, iIndex) == QValidator::Acceptable);
}
